/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.ykkj.plant.base.type.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.ykkj.plant.base.type.entity.PlantType;
import com.ykkj.plant.base.type.service.PlantTypeService;

/**
 * 植物类型管理Controller
 * @author Wrong
 * @version 2016-10-09
 */
@Controller
@RequestMapping(value = "${adminPath}/type/plantType")
public class PlantTypeController extends BaseController {

	@Autowired
	private PlantTypeService plantTypeService;
	
	@ModelAttribute
	public PlantType get(@RequestParam(required=false) String id) {
		PlantType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = plantTypeService.get(id);
		}
		if (entity == null){
			entity = new PlantType();
		}
		return entity;
	}
	
	@RequiresPermissions("type:plantType:view")
	@RequestMapping(value = {"list", ""})
	public String list(PlantType plantType, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PlantType> page = plantTypeService.findPage(new Page<PlantType>(request, response), plantType); 
		model.addAttribute("page", page);
		return "base/type/plantTypeList";
	}

	@RequiresPermissions("type:plantType:view")
	@RequestMapping(value = "form")
	public String form(PlantType plantType, Model model) {
		model.addAttribute("plantType", plantType);
		return "base/type/plantTypeForm";
	}

	@RequiresPermissions("type:plantType:edit")
	@RequestMapping(value = "save")
	public String save(PlantType plantType, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, plantType)){
			return form(plantType, model);
		}
		plantTypeService.save(plantType);
		addMessage(redirectAttributes, "保存植物类型成功");
		return "redirect:"+Global.getAdminPath()+"/type/plantType/?repage";
	}
	
	@RequiresPermissions("type:plantType:edit")
	@RequestMapping(value = "delete")
	public String delete(PlantType plantType, RedirectAttributes redirectAttributes) {
		plantTypeService.delete(plantType);
		addMessage(redirectAttributes, "删除植物类型成功");
		return "redirect:"+Global.getAdminPath()+"/type/plantType/?repage";
	}

}